<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="测试-开始" tabindex="-1"><a class="header-anchor" href="#测试-开始"><span>测试: 开始</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#environment">环境</a></li>
<li><a href="#creating-tests">创建测试</a></li>
<li><a href="#running-tests">运行测试</a>
<ul>
<li><a href="#running-tests-in-parallel">并行运行测试</a></li>
<li><a href="#reporting-test-coverage">测试覆盖率报告</a></li>
<li><a href="#profiling-tests">性能分析测试</a></li>
</ul>
</li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>Laravel 在构建时考虑到了测试。 实际上, 对 <a href="https://pestphp.com/" target="_blank" rel="noopener noreferrer">Pest</a> 和 <a href="https://phpunit.de/" target="_blank" rel="noopener noreferrer">PHPUnit</a> 的测试支持开箱即用，而且 <code v-pre>phpunit.xml</code> 文件已经为你的应用程序设置好了。框架还附带了方便的助手方法，允许你表达式地测试你的应用。</p>
<p>默认情况下，你的应用程序的 <code v-pre>tests</code> 目录包含两个目录：<code v-pre>Feature</code> 和 <code v-pre>Unit</code>。单元测试是那些专注于代码一个非常小、孤立部分的测试。事实上，大多数单元测试可能专注于单个方法。位于 「 Unit 」 测试目录中的测试不会启动你的 Laravel 应用程序，因此无法访问你的应用程序的数据库或其他框架服务。</p>
<p>功能测试可以测试你的大部分代码，包括几个对象如何互相交互，甚至是对 <code v-pre>JSON</code> 端点的完整 <code v-pre>HTTP</code> 请求。通常，你的大部分测试应该是功能测试。这些类型的测试确保你的整个系统按照预期运行。</p>
<p><code v-pre>Feature</code> 和 <code v-pre>Unit</code> 测试目录中都提供一个 <code v-pre>ExampleTest.php</code> 文件 。在安装新的 Laravel 应用程序后，执行 <code v-pre>vendor/bin/pest</code>、<code v-pre>vendor/bin/phpunit</code> 或 <code v-pre>php artisan test</code> 命令来运行你的测试。</p>
<h2 id="环境" tabindex="-1"><a class="header-anchor" href="#环境"><span>环境</span></a></h2>
<p>运行测试时，由于<code v-pre>phpunit.xml</code> 文件中定义了 <a href="https://learnku.com/docs/laravel/11.x/configurationmd/16648" target="_blank" rel="noopener noreferrer">环境变量</a> 。Laravel 会自动配置环境变量为 <code v-pre>testing</code> 。Laravel 还会在测试时自动将会话和缓存配置到 <code v-pre>array</code> 驱动程序，这意味着在测试时不会持久化会话或缓存数据。</p>
<p>你可以根据需要自由定义其他测试环境配置值。 <code v-pre>testing</code> 环境变量可以在应用程序的 <code v-pre>phpunit.xml</code> 文件中配置，但请确保在运行测试之前使用 <code v-pre>config:clear</code> Artisan 命令清除配置缓存！</p>
<h4 id="env-testing-环境配置文件" tabindex="-1"><a class="header-anchor" href="#env-testing-环境配置文件"><span><code v-pre>.env.testing</code> 环境配置文件</span></a></h4>
<p>此外，你可以在项目的根目录中创建一个 <code v-pre>.env.testing</code> 文件。在运行 Pest 和 PHPUnit 测试或执行带有 <code v-pre>--env=testing</code> 选项的 Artisan 命令时，此文件将代替 <code v-pre>.env</code> 文件使用。</p>
<h2 id="创建测试" tabindex="-1"><a class="header-anchor" href="#创建测试"><span>创建测试</span></a></h2>
<p>要创建新的测试用例，请使用 <code v-pre>make:test</code> Artisan 命令。默认情况下，测试将放置在 <code v-pre>tests/Feature</code> 目录中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:test UserTest</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>使用 <code v-pre>make:test</code> Artisan 命令来创建一个新的测试用例。默认情况下，测试将放置在 <code v-pre>tests/Feature</code> 目录：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:test UserTest <span class="token parameter variable">--unit</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>技巧<br>
可以使用 <a href="https://learnku.com/docs/laravel/11.x/artisanmd/16671" target="_blank" rel="noopener noreferrer">stub 定制</a> 来自定义测试。</p>
</blockquote>
<p>一旦测试生成，你可以使用 Pest 或 PHPUnit 那样定义测试。要运行你的测试，请从终端执行 <code v-pre>vendor/bin/pest</code>, <code v-pre>vendor/bin/phpunit</code>, 或 <code v-pre>php artisan test</code> 命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'basic'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token function">expect</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toBeTrue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Unit</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">PHPUnit<span class="token punctuation">\</span>Framework<span class="token punctuation">\</span>TestCase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ExampleTest</span> <span class="token keyword">extends</span> <span class="token class-name">TestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 一个基本的测试示例.</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_basic_test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">assertTrue</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
如果你在测试类中定义了自己的 <code v-pre>setUp</code> / <code v-pre>tearDown</code> 方法，请确保调用父类的相应 <code v-pre>parent::setUp()</code> / <code v-pre>parent::tearDown()</code> 方法。通常，你应该在你自己的 <code v-pre>setUp</code>方法开始时调用 <code v-pre>parent::setUp()</code> ，并在你的<code v-pre>tearDown</code> 方法结束时调用 <code v-pre>parent::tearDown()</code>。</p>
</blockquote>
<h2 id="运行测试" tabindex="-1"><a class="header-anchor" href="#运行测试"><span>运行测试</span></a></h2>
<p>正如前面提到的，一旦编写测试后，就可以使用 <code v-pre>pest</code> 或 <code v-pre>phpunit</code> 来运行：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/pest</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/phpunit</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>除了 <code v-pre>pest</code> 或 <code v-pre>phpunit</code> 命令，还可以使用 <code v-pre>test</code> Artisan 命令来运行测试。Artisan 测试运行器提供详细的测试报告，以便于开发和调试：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan <span class="token builtin class-name">test</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>任何传递给 <code v-pre>pest</code> 或 <code v-pre>phpunit</code> 命令的参数也可以传递给 Artisan <code v-pre>test</code> 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan <span class="token builtin class-name">test</span> <span class="token parameter variable">--testsuite</span><span class="token operator">=</span>Feature --stop-on-failure</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="并行运行测试" tabindex="-1"><a class="header-anchor" href="#并行运行测试"><span>并行运行测试</span></a></h3>
<p>默认情况下，Lavavel 和 Pest/PHPUnit 在单个进程中顺序执行测试。然而，可以通过同时在多个进程中运行测试，大大减少运行测试所需的时间。首先，应该将 <code v-pre>brianium/paratest</code> Composer 包作为 「 dev 」 依赖安装。 然后，在执行 <code v-pre>test</code> Artisan 命令时包含 <code v-pre>--parallel</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require brianium/paratest <span class="token parameter variable">--dev</span></span>
<span class="line"></span>
<span class="line">php artisan <span class="token builtin class-name">test</span> <span class="token parameter variable">--parallel</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认情况下， Laravel 将会为你的机器创建与可用 CPU 核心一样多的进程。然而，可以使用 <code v-pre>--processes</code> 选项来调整进程数：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan <span class="token builtin class-name">test</span> <span class="token parameter variable">--parallel</span> <span class="token parameter variable">--processes</span><span class="token operator">=</span><span class="token number">4</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>注意:当并行运行测试时，某些 Pest / PHPUnit 选项 (如 <code v-pre>--do-not-cache-result</code>) 可能不可用</p>
</blockquote>
<h4 id="并行测试与数据库" tabindex="-1"><a class="header-anchor" href="#并行测试与数据库"><span>并行测试与数据库</span></a></h4>
<p>只要配置了一个主数据库连接，Laravel 自动处理为每一运行额是的并行进程创建和迁移测试数据库。测试数据库会附加一个每个进程唯一的进程标记。例如，如果有两个并行测试进程，Laravel 将创建并使用 <code v-pre>your_db_test_1</code> 和 <code v-pre>your_db_test_2</code> 测试数据库。</p>
<p>默认情况下，测试数据库在调用 <code v-pre>test</code> Artisan 命令后会保留下来，以便后的 <code v-pre>test</code> 再次调用。然而，可以使用 <code v-pre>--recreate-databases</code> 选项重新创建他们：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan <span class="token builtin class-name">test</span> <span class="token parameter variable">--parallel</span> --recreate-databases</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="并行测试钩子" tabindex="-1"><a class="header-anchor" href="#并行测试钩子"><span>并行测试钩子</span></a></h4>
<p>有时，可能需要准备应用程序测试中使用的某些资源，以便它们可以安全地由多个测试进程使用。</p>
<p>使用 <code v-pre>ParallelTesting</code> facade，可以指定代码在进程或者测试用例的 <code v-pre>setUp</code> 和 <code v-pre>tearDown</code> 时执行。给定的闭包接收 <code v-pre>$token</code> 和 <code v-pre>$testCase</code> 变量，分别包含进程标记和当前测试用例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Artisan</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>ParallelTesting</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">PHPUnit<span class="token punctuation">\</span>Framework<span class="token punctuation">\</span>TestCase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">AppServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Bootstrap any application services.</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">ParallelTesting</span><span class="token operator">::</span><span class="token function">setUpProcess</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$token</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// ...</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token class-name static-context">ParallelTesting</span><span class="token operator">::</span><span class="token function">setUpTestCase</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$token</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">TestCase</span> <span class="token variable">$testCase</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// ...</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// Executed when a test database is created...</span></span>
<span class="line">        <span class="token class-name static-context">ParallelTesting</span><span class="token operator">::</span><span class="token function">setUpTestDatabase</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$database</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$token</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'db:seed'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token class-name static-context">ParallelTesting</span><span class="token operator">::</span><span class="token function">tearDownTestCase</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$token</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">TestCase</span> <span class="token variable">$testCase</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// ...</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token class-name static-context">ParallelTesting</span><span class="token operator">::</span><span class="token function">tearDownProcess</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$token</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// ...</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="访问并行测试令牌" tabindex="-1"><a class="header-anchor" href="#访问并行测试令牌"><span>访问并行测试令牌</span></a></h4>
<p>如果想要从应用程序的测试代码中任何位置访问当前的并行进程 <code v-pre>token</code> ,则可以使用 <code v-pre>token</code> 方法。此令牌是单个测试进程的唯一字符串标识符，可用于在并行测试过程中划分资源。例如，Laravel 自动用此令牌值作为每个并行测试进程创建的测试数据库名后缀：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$token</span> <span class="token operator">=</span> <span class="token class-name static-context">ParallelTesting</span><span class="token operator">::</span><span class="token function">token</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="报告测试覆盖率" tabindex="-1"><a class="header-anchor" href="#报告测试覆盖率"><span>报告测试覆盖率</span></a></h3>
<blockquote>
<p>注意：这个功能需要 <a href="https://xdebug.org/" target="_blank" rel="noopener noreferrer">Xdebug</a> 或 <a href="https://pecl.php.net/package/pcov" target="_blank" rel="noopener noreferrer">PCOV</a>.</p>
</blockquote>
<p>在运行测试时，你可能需要确定测试用例是否真的测到了某些程序代码，以及在运行测试时究竟使用了多少应用程序代码。要实现这一点，你可以在调用 <code v-pre>test</code> 命令时，增加一个 <code v-pre>--coverage</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan <span class="token builtin class-name">test</span> <span class="token parameter variable">--coverage</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="最小覆盖率阈值限制" tabindex="-1"><a class="header-anchor" href="#最小覆盖率阈值限制"><span>最小覆盖率阈值限制</span></a></h4>
<p>你可以使用 <code v-pre>--min</code> 选项来为你的应用程序定义一个最小测试覆盖率阈值。如果不满足此阈值，测试套件将失败：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan <span class="token builtin class-name">test</span> <span class="token parameter variable">--coverage</span> <span class="token parameter variable">--min</span><span class="token operator">=</span><span class="token number">80.3</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="测试性能分析" tabindex="-1"><a class="header-anchor" href="#测试性能分析"><span>测试性能分析</span></a></h3>
<p>Artisan 测试运行器还提供了一个方便的机制用于列出你的应用程序中最慢的测试。使用 <code v-pre>--profile</code> 选项调用测试命令，可以看到 10 个最慢的测试列表，这可以让你很容易地识别哪些测试可以被改进，以加快你的测试套件。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan <span class="token builtin class-name">test</span> <span class="token parameter variable">--profile</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/testingmd/16709" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/te...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/testingmd/16709" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/te...</a></p>
</blockquote>
</div></template>


